package cn.gcyl.module.biz.service.alliancemembers;

import cn.gcyl.module.biz.controller.admin.platforminformation.vo.AppPlatformInformationPageReqVO;
import cn.gcyl.module.biz.controller.admin.platforminformation.vo.PlatformInformationPageReqVO;
import cn.gcyl.module.biz.dal.dataobject.SysMemberUserDO;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.module.biz.dal.mysql.SysMemberUserMapper;
import cn.gcyl.module.biz.dal.mysql.platforminformation.PlatformInformationMapper;
import cn.gcyl.module.biz.dal.mysql.useractionlog.UserActionLogMapper;
import cn.gcyl.module.biz.service.platforminformation.PlatformInformationService;
import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.gcyl.module.biz.controller.admin.alliancemembers.vo.*;
import cn.gcyl.module.biz.dal.dataobject.alliancemembers.AllianceMembersDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.alliancemembers.AllianceMembersMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.ALLIANCE_MEMBERS_NOT_EXISTS;

/**
 * 联盟成员链接 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AllianceMembersServiceImpl implements AllianceMembersService {

    @Resource
    private AllianceMembersMapper allianceMembersMapper;
    @Resource
    private PlatformInformationMapper platformInformationMapper;
    @Resource
    private UserActionLogMapper userActionLogMapper;
    @Resource
    private SysMemberUserMapper sysMemberUserMapper;

    @Override
    public Integer createAllianceMembers(AllianceMembersSaveReqVO createReqVO) {
        // 插入
        AllianceMembersDO allianceMembers = BeanUtils.toBean(createReqVO, AllianceMembersDO.class);
        allianceMembersMapper.insert(allianceMembers);

        // 返回
        return allianceMembers.getId();
    }

    @Override
    public void updateAllianceMembers(AllianceMembersSaveReqVO updateReqVO) {
        // 校验存在
        validateAllianceMembersExists(updateReqVO.getId());
        // 更新
        AllianceMembersDO updateObj = BeanUtils.toBean(updateReqVO, AllianceMembersDO.class);
        allianceMembersMapper.updateById(updateObj);
    }

    @Override
    public void deleteAllianceMembers(Integer id) {
        // 校验存在
        validateAllianceMembersExists(id);
        // 删除
        allianceMembersMapper.deleteById(id);
    }

    @Override
        public void deleteAllianceMembersListByIds(List<Integer> ids) {
        // 删除
        allianceMembersMapper.deleteByIds(ids);
        }


    private void validateAllianceMembersExists(Integer id) {
        if (allianceMembersMapper.selectById(id) == null) {
            throw exception(ALLIANCE_MEMBERS_NOT_EXISTS);
        }
    }

    @Override
    public AllianceMembersDO getAllianceMembers(Integer id) {
        return allianceMembersMapper.selectById(id);
    }
    @Override
    public AllianceMembersDO getAppAllianceMembers(Integer id) {
        //访问一次接口浏览量加1
        AllianceMembersDO allianceMembersDO = allianceMembersMapper.selectById(id);
        allianceMembersDO.setVisitors(allianceMembersDO.getVisitors()+1);
        allianceMembersMapper.updateById(allianceMembersDO);
        Long loginUserId = getLoginUserId();
        //添加操作日志
        if (loginUserId != null) {
            UserActionLogDO userActionLogDO = new UserActionLogDO();
            userActionLogDO.setUserId(loginUserId);
            userActionLogDO.setAction("访问联盟成员:" + allianceMembersDO.getCorporateName());
            userActionLogMapper.insert(userActionLogDO);
        }
        //非会员空户只能查看一次,添加查看记录
        SysMemberUserDO sysMemberUserDO = sysMemberUserMapper.selectById(loginUserId);
        sysMemberUserDO.setViewingTime(LocalDateTime.now());
        sysMemberUserMapper.updateById(sysMemberUserDO);
        return allianceMembersDO;
    }

    @Override
    public PageResult<AllianceMembersDO> getAllianceMembersPage(AllianceMembersPageReqVO pageReqVO) {
        return allianceMembersMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<PlatformInformationDO> getAllianceMembersInfoPage(PlatformInformationPageReqVO pageReqVO) {
        return platformInformationMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<PlatformInformationDO> getAppAllianceMembersInfoPage(AppPlatformInformationPageReqVO pageReqVO) {
        return platformInformationMapper.getAppPlatformInformationPage(pageReqVO);
    }

}